package top.scauwlt.remoting.dto;

import lombok.*;
import top.scauwlt.enums.RpcResponseEnum;

import java.io.Serializable;

/**
 * @author lt_wang
 * @Date 2022/7/20 23:28
 */

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@ToString
public class RpcResponse<T> implements Serializable {
    private static final long serialVersionUID = 7226840670226486545L;
    private String requestId;
    private Integer code;
    private String message;
    private T data;

    public static <T> RpcResponse<T> success(T data, String requestId) {
        RpcResponse<T> rpcResponse = new RpcResponse<>();
        rpcResponse.setCode(RpcResponseEnum.SUCCESS.getCode());
        rpcResponse.setMessage(RpcResponseEnum.SUCCESS.getMessage());
        rpcResponse.setRequestId(requestId);
        if (data != null) {
            rpcResponse.setData(data);
        }
        return rpcResponse;
    }

    public static <T> RpcResponse<T> fail(T data, String requestId) {
        RpcResponse<T> rpcResponse = new RpcResponse<>();
        rpcResponse.setCode(RpcResponseEnum.FAIL.getCode());
        rpcResponse.setMessage(RpcResponseEnum.FAIL.getMessage());
        rpcResponse.setRequestId(requestId);
        if (data != null) {
            rpcResponse.setData(data);
        }
        return rpcResponse;
    }
}
